/**
 * @Title: WxOrderController.java
 * @Package com.lemon.controller.crwx
 * @Description: (一句话阐述):
 * @author Ran_cc
 * @date 2018年11月12日
 * @version V1.0
 */
package com.lemon.controller.crwx;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.gexin.rp.sdk.base.IPushResult;
import com.lemon.conf.MyException;
import com.lemon.conf.MyInterceptor.LoginRequired;
import com.lemon.conf.content.IdTypeHandler;
import com.lemon.conf.redis.RedisUtil;
import com.lemon.controller.BaseController;
import com.lemon.mapper.cityRun.CrShopOrdersMapper;
import com.lemon.mapper.cityRun.CrShopUserMapper;
import com.lemon.mapper.cityRun.CrShopsMapper;
import com.lemon.mapper.cityRun.CrUserTransactionRecordMapper;
import com.lemon.model.buy.PayRefundResult;
import com.lemon.model.buy.PayUnifiedorderResult;
import com.lemon.model.cityRun.CrShopOrderComment;
import com.lemon.model.cityRun.CrShopOrderGoods;
import com.lemon.model.cityRun.CrShopOrders;
import com.lemon.model.cityRun.CrShopUser;
import com.lemon.model.cityRun.CrShops;
import com.lemon.model.cityRun.CrUserTransactionRecord;
import com.lemon.model.cityRun.CrUsersInfo;
import com.lemon.service.cityRun.CrShopOrderService;
import com.lemon.service.cityRun.CrShopUserService;
import com.lemon.service.cityRun.CrShopsService;
import com.lemon.service.cityRun.CrUserService;
import com.lemon.service.cityRun.ShopPrintService;
import com.lemon.utils.CityRunUtils;
import com.lemon.utils.IpUtils;
import com.lemon.utils.ReqResult;
import com.lemon.utils.StringUtil;
import com.lemon.utils.TokenUtils;
import com.lemon.utils.appPush.AppPush;
import com.lemon.utils.pay.Constants;
import com.lemon.utils.pay.PayUtil;

import io.jsonwebtoken.Claims;

/**
 * <p>
 * Title: WxOrderController
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Company:
 * </p>
 * 
 * @author Ran_cc
 * @date 2018年11月12日
 * @版本 V 1.0
 */
@RestController
@RequestMapping("/xorder")
public class WxOrderController extends BaseController {

	@Resource
	private CrShopOrderService orderService;
	@Resource
	private CrShopsService shopsService;

	@Resource
	private ShopPrintService pService;
	@Resource
	private CrShopUserService uService;
	@Resource
	private CrUserService usService;

	@Resource
	private CrShopUserMapper uMapper;

	@Resource
	private CrShopOrdersMapper orderMapper;
	@Resource
	private CrShopsMapper shopMapper;

	@Resource
	private CrUserTransactionRecordMapper transMapper;

	@RequestMapping("/getOrdersList")
	public ReqResult getOrdersList(@ModelAttribute CrShopOrders order , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			if (StringUtil.isNotBlank(order.getStartDate())) {
				order.setWhere(" and a.status = 5  and a.create_time like '%?%' " , order.getStartDate());
			} else {
				order.setWhere(" and a.status != 6  ");
			}
			order.setOrderBy(" order by a.create_time desc");
			List<CrShopOrders> list = orderService.selectListBySelective(order);
			for (int i = 0; i < list.size(); i++) {
				CrShopOrders ord = list.get(i);
				String id = ord.getId();
				CrShopOrderGoods ogo = new CrShopOrderGoods();
				ogo.setOrderId(id);

				int time = 2;
				if (ord.getType() == 2) {
					time = 4;
				}
				if ((ord.getStatus() == 3 || ord.getStatus() == 31 || ord.getStatus() == 32)
						&& StringUtil.addDateHour(ord.getCreateTime() , time).getTime() < new Date().getTime()) {
					CrShopOrders ss = new CrShopOrders();
					ss.setId(ord.getId());
					ss.setStatus(4);
					int info = orderService.updateOrderInfo(ss);
					if (info > 0) {
						list.get(i).setStatus(4);
					}
				} else if (ord.getStatus() == 2
						&& StringUtil.addDateMinute(ord.getCreateTime() , 15).getTime() < new Date().getTime()) {
					CrShopOrders ss = new CrShopOrders();
					ss.setId(ord.getId());
					ss.setStatus(52);
					int info = orderService.updateOrderInfo(ss);
					if (info > 0) {
						list.get(i).setStatus(52);
					}
				} else if (ord.getStatus() == 4
						&& StringUtil.addDateHour(ord.getCreateTime() , 72).getTime() < new Date().getTime()) {
					if (ord.getCommentStar() == null || ord.getCommentStar() <= 0) {
						CrShopOrderComment comment = new CrShopOrderComment();
						comment.setStatus(1);
						comment.setCommentStar(5);
						comment.setShopId(ord.getShopId());
						comment.setContent("系统默认好评");
						comment.setOrderId(ord.getId());
						comment.setUserId(ord.getUserId());
						comment.setUserNickName(ord.getUserName());
						orderService.insertCommentBySelective(comment);
						list.get(i).setCommentStar(5);
					}
				}

				List<CrShopOrderGoods> li = orderService.selectOrderGoodsListBySelective(ogo);
				list.get(i).setList(li);
			}
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	@RequestMapping("/getOrdersById")
	public ReqResult getOrdersById(@ModelAttribute CrShopOrders order , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			List<CrShopOrders> list = orderService.selectListBySelective(order);
			for (int i = 0; i < list.size(); i++) {
				CrShopOrders ord = list.get(i);
				String id = ord.getId();
				CrShopOrderGoods ogo = new CrShopOrderGoods();
				ogo.setOrderId(id);
				List<CrShopOrderGoods> li = orderService.selectOrderGoodsListBySelective(ogo);
				String mark = list.get(i).getMark();

				mark = mark.trim();
				if (StringUtil.isNotBlank(mark)) {
					if (mark.indexOf("$&#$") > 0) {
						if (ord.getType() == 1) {
							mark = mark.replace("$&#$" , "/") + "份餐具";
						} else {
							mark = mark.substring(0 , mark.indexOf("$&#$"));
						}
					} else if (mark.trim().indexOf("$&#$") == 0) {
						if (ord.getType() == 1) {
							mark = mark.replace("$&#$" , "") + "份餐具";
						} else {
							mark = "";
						}
					}
					list.get(i).setMark(mark);
				}
				list.get(i).setList(li);
			}
			if (list.size() > 0) {
				CrShopOrders orders = list.get(0);
				CrShopOrders or = new CrShopOrders();
				or.setShopId(orders.getShopId());
				or.setWhere(" and a.create_time >= ? " , CityRunUtils.getBusineDateString());
				int coun = orderService.selectCountBySelective(or);
				orders.setSort(coun);
				//				CityRunUtils.setOrderTimesCookie(list.get(0).getShopId());
				//				System.out.println("done!");
				/*
				 * CrShopOrders orders = list.get(0);
				 * CrShopOrders or = new CrShopOrders();
				 * or.setShopId(orders.getShopId());
				 * or.setWhere(" and a.create_time >= ? " , CityRunUtils.getBusineDateString());
				 * int coun = orderService.selectCountBySelective(or);
				 * orders.setSort(coun);
				 * //获取打印机
				 * CrShopPrinter cp = new CrShopPrinter();
				 * cp.setShopId(orders.getShopId());
				 * cp.setStatus(1);
				 */

				/*
				 * List<CrShopPrinter> listPrinters = shopsService.selectPrintListBySelective(cp);
				 * if (listPrinters.size() > 0) {
				 * CrShopPrinter p = listPrinters.get(0);
				 * String printer = PrinterUtils.printer(orders , p.getDevNo() , p.getDevSecretKey() , 1 , 1);
				 * System.out.println(printer);
				 * }
				 */

				/*
				 * String string = RedisUtil.get("cityRun:print:print_" + orders.getShopId());
				 * if (StringUtil.isNotBlank(string)) {
				 * JSONObject js = (JSONObject) JSONObject.parse(string);
				 * JSONArray jsonArray = js.getJSONArray("list");
				 * jsonArray.add(orders);
				 * js.put("list" , jsonArray);
				 * RedisUtil.set("cityRun:print:print_" + orders.getShopId() , js);
				 * } else {
				 * JSONObject js = new JSONObject();
				 * JSONArray arr = new JSONArray();
				 * arr.add(orders);
				 * js.put("list" , arr);
				 * RedisUtil.set("cityRun:print:print_" + orders.getShopId() , js);
				 * }
				 */
			}

			reqResult.setReturnResult(list);
		} catch (Exception e) {
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	private PayUnifiedorderResult createWeixinOrder(String openid , String body , int price , String orderNo ,
			String ip , int payType) {
		String appid = Constants.WeChatAPPID;
		if (payType == 2) {
			appid = Constants.PUBLIC_ACCOUNT_APPID;
		}
		PayUnifiedorderResult order_result = PayUtil.createrOrder(appid , openid , body , price , orderNo , ip);
		return order_result;
	}

	/**
	 * <b>Description:<blockquote> 支付订单
	 * </blockquote></b>
	 * 
	 * @Title: createOrdersInfo
	 * @param order
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月12日
	 */
	@RequestMapping("/payOrdersInfo")
	public ReqResult payOrdersInfo(@ModelAttribute CrShopOrders order , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			String token = request.getHeader("wxToken");
			log.info("微信端下单:wxToken={}" , token);
			String openId = "";
			if (StringUtil.isNotBlank(token)) {
				Claims parseJWT = TokenUtils.parseJWT(token);
				String info = parseJWT.getId();
				log.info("解析后的token={}" , info);

				if (StringUtil.isNotBlank(info)) {
					String[] split = info.split("\\|");
					openId = split[0];
				}
				log.info("解析后的openId={}" , openId);
				if (StringUtil.isEmpty(openId)) {
					reqResult.setReturnResult("token is null " , ReqResult.code_no_permission);
					return reqResult;
				}
			}
			String ip = IpUtils.getIpAddr(request);
			List<CrShopOrders> list = orderService.selectListBySelective(order);

			if (list.size() <= 0) {
				reqResult.setReturnResult("获取订单详情失败" , ReqResult.code_error);
				return reqResult;
			}
			CrShopOrders cs = list.get(0);
			order = new CrShopOrders();
			order.setId(cs.getId());
			order.setOrderNo(StringUtil.createSerialNoCityRun());
			int i = orderService.updateOrderInfo(order);

			//调用微信支付
			PayUnifiedorderResult result = createWeixinOrder(openId , "城跑 - 下单支付" , cs.getPayPrice() ,
					order.getOrderNo() , ip , 1);
			if (result != null) {
				result.setOrderNo(cs.getOrderNo());
			}
			reqResult.setReturnResult(result);
		} catch (Exception e) {
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	@RequestMapping("/updateOrdersInfo")
	public ReqResult updateOrdersInfo(@ModelAttribute CrShopOrders order , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			String token = request.getHeader("wxToken");
			log.info("微信端下单:wxToken={}" , token);
			String openId = "";

			if (StringUtil.isNotBlank(token)) {
				Claims parseJWT = TokenUtils.parseJWT(token);
				String info = parseJWT.getId();
				log.info("解析后的token={}" , info);

				if (StringUtil.isNotBlank(info)) {
					String[] split = info.split("\\|");
					openId = split[0];
				}
				log.info("解析后的openId={}" , openId);
				if (StringUtil.isEmpty(openId)) {
					reqResult.setReturnResult("token is null " , ReqResult.code_no_permission);
					return reqResult;
				}
			}

			//删除订单 
			if (order.getStatus() == 6) {
				CrShopOrders or = new CrShopOrders();
				or.setId(order.getId());
				or.setStatus(6);
				int row = orderService.updateOrderInfo(or);
				if (row > 0) {
					reqResult.setReturnResult(row);
					return reqResult;
				}
				reqResult.setReturnResult("删除失败" , ReqResult.code_fail);
				return reqResult;
			}
			CrShopOrders order2 = orderService.selectByOrderId(order.getId());
			//order2.setPayPrice(1);
			//修改订单状态 = 取消
			if (order.getStatus() == 5 && order2.getStatus() == 2) {
				//取消订单 并图款
				if (order2.getPayType() == 1) {

					PayRefundResult result2 = PayUtil.refund(Constants.WeChatAPPID , order2.getOrderNo() ,
							order2.getOrderNo() , order2.getPayPrice() , order2.getPayPrice() , "");
					CrShopOrders or1 = new CrShopOrders();
					or1.setOrderNo(order2.getOrderNo());
					or1.setStatus(51);
					int row = orderMapper.updateOrderStatByPayOk(or1);
					if (row > 0) {
						//修改库存数量
						order2.setStatus(51);
						orderService.updateCelGoodsNum(order2);
						//添加订单流水线
						/*
						 * or1 = new CrShopOrders();
						 * or1.setOrderNo(order2.getOrderNo());
						 * CrShopOrders order1 = orderMapper.selectBySelective(or1);
						 * CrShopUser u = new CrShopUser();
						 * u.setWhere(" and b.id = ? " , IdTypeHandler.decode(order1.getShopId()));
						 * CrShopUser user = uMapper.selectBySelective(u);
						 * CrShops cs = new CrShops();
						 * cs.setShopUserId(user.getId());
						 * CrShops shop = shopMapper.selectBySelective(cs);
						 * CrUserTransactionRecord tran = new CrUserTransactionRecord();
						 * tran.setShopUserId(user.getId());
						 * tran.setShopAccount(user.getUserAccount());
						 * tran.setShopTitle(shop.getShopTitle());
						 * tran.setShopId(shop.getId());
						 * tran.setSerialNumber(order2.getOrderNo());
						 * tran.setAmount(order2.getPayPrice());
						 * tran.setStatus(1);
						 * tran.setType(1);
						 * tran.setTranType(1);
						 * transMapper.insertSelective(tran);
						 */
						//修改账户信息
						//					u = new CrShopUser();
						//					u.setId(tran.getShopUserId());
						//					u.setAmount(-tran.getAmount());
						//					int selective = uMapper.updateByPrimaryKeySelective(u);
						//发送信息到app
						try {
							CrShopUser u = new CrShopUser();
							u.setWhere(" and b.id = ? " , IdTypeHandler.decode(order2.getShopId()));
							CrShopUser user = uMapper.selectBySelective(u);
							log.info("开始推送取消消息");
							AppPush push = new AppPush();
							push.setCid(user.getCid());
							push.setContent(order2.getOrderNo());
							push.setTitle("订单取消");
							push.setType(11);
							push.setOrderType(order2.getType());
							push.setText("您有一个订单取消,点击查看");
							push.setOrderType(order2.getType());
							IPushResult pushMsgToSingle = AppPush.pushMsgToSingle(push);
							log.info(pushMsgToSingle.getResponse().toString());
						} catch (Exception e) {
						}
					}
				} else {
					//现金支付 直接该状态
					CrShopOrders or = new CrShopOrders();
					or.setId(order.getId());
					or.setStatus(51);
					int row = orderService.updateOrderInfo(or);
					if (row > 0) {
						reqResult.setReturnResult(row);
					} else {
						reqResult.setReturnResult("修改失败" , ReqResult.code_fail);
					}
				}
			} else if (order.getStatus() == 5 && order2.getStatus() == 1) {

				CrShopOrders or = new CrShopOrders();
				or.setId(order.getId());
				or.setStatus(51);
				int row = orderService.updateOrderInfo(or);

				if (row > 0) {
					reqResult.setReturnResult(row);
				} else {
					reqResult.setReturnResult("修改失败" , ReqResult.code_fail);
				}
			} else {
				reqResult.setReturnResult("订单状态已变更,取消订单失败" , ReqResult.code_fail);
			}

			//如果状态是取消状态 , 则修改菜品数量
			//			if (order.getStatus() == 5) {
			//				order.getId();
			//			}

			/*
			 * try {
			 * log.info("开始发送短信到客户端");
			 * CrShopUser u = new CrShopUser();
			 * u.setWhere(" and b.id = ? " , IdTypeHandler.decode(order.getShopId()));
			 * CrShopUser user = userMapper.selectBySelective(u);
			 * String cid = user.getCid();
			 * AppPush push = new AppPush();
			 * push.setCid(cid);
			 * push.setContent(order2.getOrderNo());
			 * push.setTitle("取消订单通知");
			 * push.setType(11);
			 * push.setText("您有一个取消订单,点击查看");
			 * push.setOrderType(order.getType());
			 * IPushResult ret = AppPush.pushMsgToSingle(push);
			 * log.info(ret.getResponse().toString());
			 * } catch (Exception ez) {
			 * MyException.writeErr(ez , "发送订单短信到app失败");
			 * }
			 */
		} catch (Exception e) {
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	@RequestMapping("/celOrdersInfo")
	public ReqResult celOrdersInfo(@ModelAttribute CrShopOrders order , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();

		String token = request.getHeader("wxToken");
		log.info("微信端下单:wxToken={}" , token);
		String openId = "";
		if (StringUtil.isNotBlank(token)) {
			Claims parseJWT = TokenUtils.parseJWT(token);
			String info = parseJWT.getId();
			log.info("解析后的token={}" , info);
			if (StringUtil.isNotBlank(info)) {
				String[] split = info.split("\\|");
				openId = split[0];
			}
			log.info("解析后的openId={}" , openId);
			if (StringUtil.isEmpty(openId)) {
				reqResult.setReturnResult("token is null " , ReqResult.code_no_permission);
				return reqResult;
			}
		} else {
			reqResult.setReturnResult("token is null " , ReqResult.code_no_permission);
			return reqResult;
		}

		try {
			List<CrShopOrders> list = orderService.selectListBySelective(order);
			if (list.size() <= 0) {
				reqResult.setReturnResult("获取订单详情失败" , ReqResult.code_error);
				return reqResult;
			}
			CrShopOrders cs = list.get(0);
			if (cs.getStatus() == 1) {
				CrShopOrders css = new CrShopOrders();
				css.setStatus(5);
				css.setId(cs.getId());
				orderService.updateOrderInfo(order);
			} else {
				PayRefundResult result2 = PayUtil.refund(Constants.WeChatAPPID , cs.getOrderNo() , cs.getOrderNo() , 1 ,
						1 , "");
				reqResult.setReturnResult(result2);
			}
			return reqResult;
		} catch (Exception e) {
			MyException.writeErr(e , "取消订单失败");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 创建订单
	 * </blockquote></b>
	 * 
	 * @Title: createOrdersInfo
	 * @param order
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月12日
	 */
	@RequestMapping("/createOrdersInfo")
	public ReqResult createOrdersInfo(@ModelAttribute CrShopOrders order , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();

		String token = request.getHeader("wxToken");
		log.info("微信端下单:wxToken={}" , token);
		String openId = "";

		/*
		 * if (true) {
		 * // 退款
		 * // 2018117013412994
		 * // 2018110649991864
		 * PayRefundResult result2 = PayUtil.refund(Constants.WeChatAPPID , "2018110649991864" , "2018110649991864" ,
		 * 1 , 1 , "");
		 * reqResult.setReturnResult(result2);
		 * return reqResult;
		 * }
		 */

		if (StringUtil.isNotBlank(token)) {
			Claims parseJWT = TokenUtils.parseJWT(token);
			String info = parseJWT.getId();
			log.info("解析后的token={}" , info);
			if (StringUtil.isNotBlank(info)) {
				String[] split = info.split("\\|");
				openId = split[0];
				CrUsersInfo usersInfo = new CrUsersInfo();
				try {
					log.info("通过openid 查询用户信息");
					List<CrUsersInfo> list = usService.selectListBySelective(usersInfo);
					log.info(list.get(0).toString());
					if (list.size() > 0) {
						order.setUserId(list.get(0).getId());
					} else {
						reqResult.setReturnResult("token is null " , ReqResult.code_no_permission);
						return reqResult;
					}
				} catch (Exception e) {
					MyException.writeErr(e , "");
				}
			}
			log.info("解析后的openId={}" , openId);
			if (StringUtil.isEmpty(openId)) {
				reqResult.setReturnResult("token is null " , ReqResult.code_no_permission);
				return reqResult;
			}
		} else {
			reqResult.setReturnResult("token is null " , ReqResult.code_no_permission);
			return reqResult;
		}

		try {
			//			openId = "oVNOo5V_-jrrGwR3pen2DcuETLsI";
			//order.setStatus(1); // 未支付 , 等微信支付成功回调地址里面在修改成已支付
			////////////////////////////////////////////////////////////
			order.setSendTime("立即送达");
			String orderNo = StringUtil.createSerialNoCityRun();
			order.setOrderNo(orderNo);
			int row = 0;
			order.setStartDate(CityRunUtils.getBusineDateString());
			//将地理位置转化成经纬度
			/*
			 * String address = PlatUtils.getLocationByAddress(order.getUserAddress());
			 * if (StringUtil.isEmpty(address)) {
			 * reqResult.setReturnResult(" " , ReqResult.code_no_permission);
			 * return reqResult;
			 * } else {
			 * String[] split = address.split(",");
			 * order.setLongitude(split[0]);
			 * order.setLatitude(split[1]);
			 * }
			 */

			row = orderService.createOrdersInfo(order);
			if (row > 0) {
				if (order.getPayType() == 1) {
					if (order.getPayPrice() > 0) {
						String ip = IpUtils.getIpAddr(request);
						//调用微信支付
						PayUnifiedorderResult result = createWeixinOrder(openId , "城跑 - 下单支付" , order.getPayPrice() ,
								orderNo , ip , 1);
						if (result != null) {
							result.setOrderNo(orderNo);
							//						formIdService.storeFormId(userid , openid , result.getPrepayId());
						}
						reqResult.setReturnResult(result);
						return reqResult;
					} else {
						reqResult.setReturnResult(1);
						return reqResult;
					}
				} else {
					reqResult.setReturnResult(row);
					return reqResult;
				}
			} else {
				reqResult.setReturnResult("订单创建失败" , ReqResult.code_error);
				return reqResult;
			}

		} catch (Exception e) {
			reqResult.setReturnResult(e.getMessage() , ReqResult.code_error);
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> app创建续费订单
	 * </blockquote></b>
	 * 
	 * @Title: createOrdersInfo
	 * @param order
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月12日
	 */
	@RequestMapping("/createRenewalOrdersInfo")
	public ReqResult createRenewalOrdersInfo(String shopId , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();

		String token = request.getHeader("wxToken");
		log.info("App端续费:wxToken={}" , token);
		String openId = "";
		try {
			CrShops cs = new CrShops();
			cs.setId(shopId);
			CrShops shops = shopsService.selectShopAndUserBySel(cs);
			//判断当前店铺是否是首年

			//查询是否有支付过
			CrUserTransactionRecord record = new CrUserTransactionRecord();
			record.setShopUserId(shops.getShopUserId());
			record.setTranType(3);
			List<CrUserTransactionRecord> list = transMapper.selectListBySelective(record);
			int price = 0;

			//			int days = StringUtil.getDistanceDays(shops.getCreateTime() , shops.getServerEndTime());
			//			String string = RedisUtil.get(RedisUtil.key_before + "setting");
			//			JSONObject parse = (JSONObject) JSONObject.parse(string);
			//			JSONObject js = parse.getJSONObject("sets");
			//			JSONObject js2 = js.getJSONObject("sysSet");
			//			if (days > 300) {
			//				if (shops.getShopType() == 1) {
			//					price = Integer.parseInt(js2.get("nextYear1").toString());
			//				} else {
			//					price = Integer.parseInt(js2.get("nextYear2").toString());
			//				}
			//			} else {
			//				if (shops.getShopType() == 1) {
			//					price = Integer.parseInt(js2.get("firstYear1").toString());
			//				} else {
			//					price = Integer.parseInt(js2.get("firstYear2").toString());
			//				}
			//			}

			String string = RedisUtil.get(RedisUtil.key_before + "setting");
			JSONObject parse = (JSONObject) JSONObject.parse(string);
			JSONObject js = parse.getJSONObject("sets");
			JSONObject js2 = js.getJSONObject("sysSet");
			if (shops.getShopType() == 1) {
				if (list.size() > 0) {
					//续费
					price = Integer.parseInt(js2.get("nextYear1").toString());
				} else {
					//首年
					price = Integer.parseInt(js2.get("firstYear1").toString());
				}
			} else {
				if (list.size() > 0) {
					//续费
					price = Integer.parseInt(js2.get("nextYear2").toString());
				} else {
					//首年
					price = Integer.parseInt(js2.get("firstYear2").toString());
				}
			}

			String orderNo = StringUtil.createSerialNoCityRun();
			String ip = IpUtils.getIpAddr(request);
			//调用微信支付
			PayUnifiedorderResult result = PayUtil.createrOrderApp(Constants.WeChatAPPID_APP , "城跑 - 续费支付" , price ,
					orderNo , ip , shopId);
			if (result != null) {
				result.setOrderNo(orderNo);
			}
			reqResult.setReturnResult(result);
			return reqResult;

		} catch (Exception e) {
			reqResult.setReturnResult(e.getMessage() , ReqResult.code_error);
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 获取要打印的订单
	 * </blockquote></b>
	 * 
	 * @Title: createOrdersInfo
	 * @param order
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月12日
	 */
	@RequestMapping("/getPrinterOrder")
	public ReqResult getPrinterOrder(@ModelAttribute CrShopOrders order , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			//页面需要判断打印机是wifi类型还是 usb类型的, 如果是wifi类型的此接口不需要, 如果还是usb类型的调用此接口
			String string = RedisUtil.get("cityRun:print:print_" + order.getShopId());
			RedisUtil.del("cityRun:print:print_" + order.getShopId());
			//CityRunUtils.setOrderTimesCookie(order.getShopId());
			reqResult.setReturnResult(JSONObject.parse(string));
		} catch (Exception e) {
			reqResult.setReturnResult("err" , ReqResult.code_error);
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	@LoginRequired
	@RequestMapping("/doPrinterOrder")
	public ReqResult doPrinterOrder(@ModelAttribute CrShopOrders order , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {

			List<CrShopOrders> list = orderService.selectListBySelective(order);
			for (int i = 0; i < list.size(); i++) {
				CrShopOrders ord = list.get(i);
				String id = ord.getId();
				CrShopOrderGoods ogo = new CrShopOrderGoods();
				ogo.setOrderId(id);
				List<CrShopOrderGoods> li = orderService.selectOrderGoodsListBySelective(ogo);
				list.get(i).setList(li);
			}
			if (list.size() > 0) {
				CrShopOrders orders = list.get(0);
				pService.printerInfo(orders.getShopId() , orders.getId());
			}
		} catch (Exception e) {
			reqResult.setReturnResult("err" , ReqResult.code_error);
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	@RequestMapping("/doPrinterOrderApp")
	public ReqResult doPrinterOrderApp(@ModelAttribute CrShopOrders order , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {

			//			if (true) {
			//				return reqResult;
			//			}
			log.info("app端调用wifi打印start...");
			String dd = request.getParameter("orderCreateTime");
			if (StringUtil.isNotBlank(dd)) {
				order.setCreateTime(StringUtil.StringToDate(dd , 4));
			}
			List<CrShopOrders> list = orderService.selectListBySelective(order);
			for (int i = 0; i < list.size(); i++) {
				CrShopOrders ord = list.get(i);
				String id = ord.getId();
				CrShopOrderGoods ogo = new CrShopOrderGoods();
				ogo.setOrderId(id);
				List<CrShopOrderGoods> li = orderService.selectOrderGoodsListBySelective(ogo);
				list.get(i).setList(li);
			}
			if (list.size() > 0) {
				CrShopOrders orders = list.get(0);
				pService.printerInfo(orders.getShopId() , orders.getId());
			}
			log.info("app端调用wifi打印end...");
		} catch (Exception e) {
			reqResult.setReturnResult("err" , ReqResult.code_error);
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	@RequestMapping("/addOrderCommentInfo.do")
	public ReqResult addOrderCommentInfo(@ModelAttribute CrShopOrderComment comment , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			comment.setStatus(1);
			int row = orderService.insertCommentBySelective(comment);
			reqResult.setReturnResult(row);
		} catch (Exception e) {
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	@RequestMapping("/getOrderCommentInfoByOrderId")
	public ReqResult getOrderCommentInfoByOrderId(@ModelAttribute CrShopOrderComment comment ,
			HttpServletRequest request , HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			CrShopOrderComment row = orderService.selectCommentBySelective(comment);
			reqResult.setReturnResult(row);
		} catch (Exception e) {
			MyException.writeErr(e , "");
		}
		return reqResult;
	}
}
